package com.xdf.xzymanagementsystem.service.impl;

import com.xdf.xzymanagementsystem.base.BaseController;
import com.xdf.xzymanagementsystem.dto.ExcelInputParamDTO;
import com.xdf.xzymanagementsystem.service.base.BaseService;
import com.xdf.xzymanagementsystem.util.ExcelUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xdf.xzymanagementsystem.entity.CurrencyType;

import java.util.List;

import com.xdf.xzymanagementsystem.dao.CurrencyTypeMapper;
import com.xdf.xzymanagementsystem.service.CurrencyTypeService;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description : com.xdf.xzymanagementsystem.service.impl(业务层实现类,该类为生成器自动生成).
 * @Author : 张柯
 * @Date : 2021-04-14 09:25:22
 */
@Service
public class CurrencyTypeServiceImpl extends BaseController implements CurrencyTypeService {
    @Autowired
    private CurrencyTypeMapper dao;
    @Autowired
    private ExcelUtils excelUtils;

    /**
     * @Describe 新增方法
     * @author 张柯
     * @Date 2021-04-14 09:25:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(CurrencyType record) throws Exception {
        return dao.insert(record);
    }

    /**
     * @Describe 选择新增
     * @author 张柯
     * @Date 2021-04-14 09:25:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(CurrencyType record) throws Exception {
        return dao.insertSelective(record);
    }

    /**
     * @Describe 批量新增
     * @author 张柯
     * @Date 2021-04-14 09:25:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<CurrencyType> list) throws Exception {
        return dao.multiInsert(list);
    }

    /**
     * @Describe 修改
     * @author 张柯
     * @Date 2021-04-14 09:25:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(CurrencyType record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 选择修改
     * @author 张柯
     * @Date 2021-04-14 09:25:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(CurrencyType record) throws Exception {
        return dao.updateByPrimaryKeySelective(record);
    }

    /**
     * @Describe 修改
     * @author 张柯
     * @Date 2021-04-14 09:25:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(CurrencyType record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 批量修改
     * @author 张柯
     * @Date 2021-04-14 09:25:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiUpdate(List<CurrencyType> list) throws Exception {
        return dao.multiUpdate(list);
    }

    /**
     * @Describe 根据主键查询
     * @author 张柯
     * @Date 2021-04-14 09:25:22
     */
    @Override
    public CurrencyType selectByPrimaryKey(String id) throws Exception {
        return dao.selectByPrimaryKey(id);
    }

    /**
     * @Describe 根据主键删除
     * @author 张柯
     * @Date 2021-04-14 09:25:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(String id) throws Exception {
        return dao.deleteByPrimaryKey(id);
    }

    /**
     * @Describe 根据Entity查询
     * @author 张柯
     * @Date 2021-04-14 09:25:22
     */
    @Override
    public List<CurrencyType> listQuery(CurrencyType record) throws Exception {
        return dao.selectByParam(record);
    }

    /**
     * 上传
     *
     * @param file
     * @param param
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public int upload(MultipartFile file, ExcelInputParamDTO param, HttpServletRequest request) throws Exception {
        String fileName = file.getOriginalFilename();
        Assert.state(isExcel(fileName), "不是Excel文件");
        if (param != null) {
            param.setFileName(fileName);
            param.setFieldSetting(conventToMap(request));
        }
        List<CurrencyType> list = excelUtils.readExcel(file.getInputStream(), param, CurrencyType.class);
        int result = 0;
        if (CollectionUtils.isNotEmpty(list)) {
            for (CurrencyType currencyType : list) {
                result += dao.insertDumplicateKeyUpdate(currencyType);
            }
        }
        return result;
    }
}